package com.ama.stock;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Utils {

	static float getTurboQuote(){
		String servlet = "http://www.produitsdebourse.bnpparibas.fr/fr/Produit.aspx?ProductId=14954";
		URL url;
		try {
			url = new URL(servlet);
			HttpURLConnection conn = (HttpURLConnection)url.openConnection();
			BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			String inputLine;
			String html = "";
			while ((inputLine = reader.readLine()) != null) {
				html = html + inputLine;
			}
			Document doc = Jsoup.parse(html);
			//Elements el = doc.select("#ctl00_content_LblCotations");
			Element content = doc.getElementById("ctl00_content_LblCotations");
			String result = content.html().split(" ")[1].replaceAll(",",".");
			return Float.parseFloat(result);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
	}
	
	static String getQuote(){
		String servlet = "http://www.forexpros.fr/indices/france-40-technical";
		URL url;
		try {
			url = new URL(servlet);
			HttpURLConnection conn = (HttpURLConnection)url.openConnection();
			conn.setRequestProperty("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1309.0 Safari/537.17");
			BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			String inputLine;
			String html = "";
			while ((inputLine = reader.readLine()) != null) {
				html = html + inputLine;
			}
			Document doc = Jsoup.parse(html);
			//Elements el = doc.select("#ctl00_content_LblCotations");
			Element content = doc.getElementById("last_last");
			Elements elts = doc.getElementsByClass("studySummaryOval");
			//return html;
			return content.html()+";"+elts.get(0).html();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
	}
}
